package com.zhao.dota.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhao.dota.contant.RoleCodeCont;
import com.zhao.dota.mapper.RoleMapper;
import com.zhao.dota.mapper.UserRoleMapper;
import com.zhao.dota.model.Role;
import com.zhao.dota.model.User;
import com.zhao.dota.model.UserRole;
import com.zhao.dota.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public List<Role> findByUserId(Integer userId) {
        return baseMapper.findByUserId(userId);
    }

    @Override
    public void addPlayerRole(User user) {
        Role role = findByCode(RoleCodeCont.PLAYER);
        UserRole userRole = new UserRole();
        userRole.setRoleId(role.getId());
        userRole.setUserId(user.getId());
        userRoleMapper.insert(userRole);
    }

    private Role findByCode(String code) {
        LambdaQueryWrapper<Role> query = Wrappers.lambdaQuery(Role.class).eq(Role::getCode, code);
        return getOne(query);
    }

}
